﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization;
using System.ServiceModel.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace OA.Web.Project.Handler
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProjectStageInfo : IHttpHandler
    {
         List<TreeModel> list = new List<TreeModel>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.ContentEncoding = Encoding.Default;
            // string tree = "[{\"id\":1,\"text\":\"Folder1\",\"iconCls\":\"icon-save\",\"children\":[{\"text\":\"File1\",\"checked\":true},{\"text\":\"Books\",\"state\":\"open\",\"attributes\":{\"url\":\"/demo/book/abc\",\"price\":100},\"children\":[{\"text\":\"PhotoShop\",\"checked\":true},{\"id\": 8,\"text\":\"Sub Bookds\",\"state\":\"closed\"}]}]},\"text\":\"Languages\",\"state\":\"closed\", \"children\":[{\"text\":\"Java\"},{ \"text\":\"C#\"}]}]";
            // HttpContext.Current.Response.Write(tree);
            // context.Response.Write(tree);
            //context.Response.End();
            string type = context.Request.QueryString["type"];
            if (type == "add")
            {

            }
            else
                InitData();
            DataContractJsonSerializer dcj = new DataContractJsonSerializer(list.GetType());
            using (MemoryStream ms = new MemoryStream())
            {
                dcj.WriteObject(ms, list);
                string json = Encoding.UTF8.GetString(ms.GetBuffer());
                json = json.ToLower().Replace("iconcls", "iconCls");
                context.Response.Write(json);
            }
        }

        private  void InitData()
        {
            TreeModel t1 = new TreeModel { Text = "前期准备", Checked = false, IconCls = "icon-save" };
            TreeModel t3 = new TreeModel { Text = "前期准备", Checked = true, Children = new TreeModel[] { t1, t1 } };
            TreeModel t2 = new TreeModel { Id = 1, Text = "初步设计阶段", IconCls = "icon-save", Children = new TreeModel[] { t1, t3 } };
            // TreeModel[] tms = new TreeModel[3];
            list.Add(t1);
            list.Add(t2);
            list.Add(t3);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
    public class TreeModel
    {
        public int Id { get; set; }
        public string Text { get; set; }
        public string IconCls { get; set; }
        public TreeModel[] Children { get; set; }
        public bool Checked { get; set; }
    }

}
